﻿using Kingdee.BOS.App.Data;
using Kingdee.BOS.Core.DynamicForm.PlugIn.Args;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.List.PlugIn;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LiMinCRMPlugin
{
    [Description("调拨申请单批量更改金额字段")]
    [HotUpdate]
    public class TransferApplyCilckEdit : AbstractListPlugIn
    {
        [Description("1调拨申请单批量更改金额字段")]
        [HotUpdate]
        public class TransferApplyCilck : AbstractListPlugIn
        {
            public override void BarItemClick(BarItemClickEventArgs e)
            {
                base.BarItemClick(e);

                if (e.BarItemKey.Equals("tbB_BOS_UpdateAmount", StringComparison.OrdinalIgnoreCase))
                {
                    //查询物料在组织间结算价目标中单据状态，行状态，是否否审核，是否在生效日和失效日期间
                    string SQL = string.Format(@"SELECT A.FBILLNO,B.FENTRYID,B.FMATERIALID,e.FPRICE,E.FTAXPRICE  FROM T_STK_STKTRANSFERAPP A
                                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON A.FID=B.FID
                                                    INNER JOIN T_BD_MATERIAL C ON B.FMATERIALID=C.FMATERIALID
                                                    INNER JOIN T_BD_MATERIAL CD ON C.FNUMBER = CD.FNUMBER AND B.FSTOCKORGID=CD.FUSEORGID
                                                    INNER JOIN T_BD_MATERIALBASE D on CD.FMATERIALID = D.FMATERIALID AND  D.FERPCLSID='2'
                                                    INNER JOIN T_IOS_PRICELISTENTRY E ON E.FMATERIALID= B.FMATERIALID AND  GETDATE() BETWEEN E.FEFFECTIVEDATE AND E.FEXPRIYDATE AND E.FROWAUDITSTATUS='A' 
                                                    INNER JOIN T_IOS_PRICELIST F ON E.FID=F.FID AND GETDATE() BETWEEN F.FEFFECTIVEDATE AND F.FEXPIRYDATE AND F.FDOCUMENTSTATUS='C'
                                                    WHERE FTRANSTYPE='OverOrgTransfer' AND F_QLTK_Combo_tzk='2' order by F.FCREATEDATE desc");
                    DataTable dt = DBUtils.ExecuteDataSet(this.Context, SQL).Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            string FENTRYID = dt.Rows[j]["FENTRYID"].ToString();
                            double FPRICE = double.Parse(dt.Rows[0]["FPRICE"].ToString());//含税单价
                            double FTaxPrice = double.Parse(dt.Rows[0]["FTAXPRICE"].ToString());//含税单价
                            string SQL1 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_RDLV_HSDJ='" + FPRICE + "',F_QLTK_TaxPrice='" + FTaxPrice + "' WHERE FENTRYID='" + FENTRYID + "'");
                            DBUtils.Execute(this.Context, SQL1);
                            string SQL2 = string.Format(@"UPDATE T_STK_STKTRANSFERAPPENTRY SET F_QLTK_Amount_qtr=F_RDLV_HSDJ*FBASEQTY,F_QLTK_AllAmount=F_QLTK_TaxPrice*FBASEQTY  WHERE FENTRYID='" + FENTRYID + "'");
                            DBUtils.Execute(this.Context, SQL2);
                        }
                    }
                    //取调出组织的最后期初结存价格并更新到调拨申请单上
                    string sql = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFERAPPENTRY SET F_RDLV_HSDJ = C.单价
                                FROM 
                                (SELECT   
                                         C.FFINORGID AS FORGID,  
                                         M.FMASTERID,  
                                         M.FNUMBER,  
                                         O.FYEAR,  
                                         O.FPERIOD,  
                                        SUM(CASE  WHEN HB.FENDINITKEY = 0 THEN HB.FQTY ELSE 0 END) 期初数, 
                                        SUM(CASE  WHEN HB.FENDINITKEY = 0 THEN HB.FAMOUNT ELSE 0 END) 期初金额,
                                        SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FQTY ELSE 0 END) AS 期末数,
                                        SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FAMOUNT ELSE 0 END) AS 期末金额
                                         SUM(CASE WHEN HB.FENDINITKEY=1 THEN HB.FAMOUNT/HB.FQTY ELSE 0 END) AS 单价 

                                     FROM   
                                         T_HS_BALANCE HB  
                                     LEFT JOIN   
                                         T_HS_STOCKDIMENSION S ON HB.FDIMEENTRYID=S.FENTRYID  
                                     LEFT JOIN   
                                         T_HS_CALDIMENSIONS C ON HB.FDIMENSIONID=C.FDIMENSIONID  
                                     LEFT JOIN   
                                         T_HS_OUTACCTG O ON HB.FID=O.FID  
                                     LEFT JOIN   
                                         T_BD_MATERIAL M ON S.FMASTERID=M.FMASTERID AND C.FFINORGID=M.FUSEORGID  
                                     LEFT JOIN   
                                         T_ORG_ORGANIZATIONS_L L ON L.FORGID=C.FFINORGID AND L.FLOCALEID=2052  
                                     LEFT JOIN   
                                         T_BD_STOCK AS ST ON ST.FSTOCKID =S.FSTOCKID  
                                     WHERE   
                                         O.FYEAR='2024'  
                                     GROUP BY   
                                         C.FFINORGID, M.FMASTERID, O.FYEAR, O.FPERIOD, M.FNUMBER  
                                    ) C   
                                INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON C.FORGID=B.FSTOCKORGID  
                                INNER JOIN T_STK_STKTRANSFERAPP A ON A.FID=B.FID AND A.F_QLTK_Combo_tzk='2' AND A.FTRANSTYPE='OverOrgTransfer'
                                INNER JOIN T_BD_MATERIALBASE D ON D.FMATERIALID = B.FMATERIALID AND D.FERPCLSID='1'
                                INNER JOIN T_BD_MATERIAL E ON B.FMATERIALID = E.FMATERIALID AND C.FMASTERID = E.FMASTERID");
                    DBUtils.Execute(this.Context, sql);
                    //更新含税金额
                    string sql1 = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFERAPPENTRY SET  F_QLTK_AMOUNT_QTR = B.F_RDLV_HSDJ* B.FBASEQTY
                                    FROM T_STK_STKTRANSFERAPP A
                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON A.FID = B.FID
                                    INNER JOIN T_BD_MATERIAL C ON B.FMATERIALID = C.FMATERIALID 
                                    INNER JOIN T_BD_MATERIALBASE  D ON D.FMATERIALID = B.FMATERIALID AND D.FERPCLSID='1' 
                                    WHERE A.F_QLTK_Combo_tzk='2' AND A.FTRANSTYPE='OverOrgTransfer'");
                    DBUtils.Execute(this.Context, sql1);

                    string sql5 = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFERAPPENTRY SET  F_QLTK_TaxPrice = B.F_RDLV_HSDJ*(1+F_QLTK_SL/100.0)
                                    FROM T_STK_STKTRANSFERAPP A
                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON A.FID = B.FID
                                    INNER JOIN T_BD_MATERIAL C ON B.FMATERIALID = C.FMATERIALID 
                                    INNER JOIN T_BD_MATERIALBASE  D ON D.FMATERIALID = B.FMATERIALID AND D.FERPCLSID='1' 
                                    WHERE A.F_QLTK_Combo_tzk='2' AND A.FTRANSTYPE='OverOrgTransfer'");
                    DBUtils.Execute(this.Context, sql5);
                    string sql4 = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFERAPPENTRY SET  F_QLTK_AllAmount = B.F_QLTK_TaxPrice* B.FBASEQTY
                                    FROM T_STK_STKTRANSFERAPP A
                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY B ON A.FID = B.FID
                                    INNER JOIN T_BD_MATERIAL C ON B.FMATERIALID = C.FMATERIALID 
                                    INNER JOIN T_BD_MATERIALBASE  D ON D.FMATERIALID = B.FMATERIALID AND D.FERPCLSID='1' 
                                    WHERE A.F_QLTK_Combo_tzk='2' AND A.FTRANSTYPE='OverOrgTransfer'");
                    DBUtils.Execute(this.Context, sql4);
                    //更新分布式调出单的含税金额，含税单价
                    string sql2 = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFEROUTENTRY SET F_QLTK_HSPRICE = E.F_RDLV_HSDJ,F_QLTK_HSAmount=E.F_QLTK_AMOUNT_QTR
                                FROM T_STK_STKTRANSFEROUT  A --分布式调出单
                                INNER JOIN T_STK_STKTRANSFEROUTENTRY B ON A.FID = B.FID --分布式调出单明细
                                INNER JOIN T_STK_STKTRANSFEROUTENTRY_LK C ON B.FENTRYID=C.FENTRYID AND FRULEID='STK_TRANSFERAPPLY-STK_TRANSFEROUT'--分布式调出单明细关联表
                                INNER JOIN T_STK_STKTRANSFERAPP D ON C.FSBILLID = D.FID AND D.F_QLTK_Combo_tzk='2' AND D.FTRANSTYPE='OverOrgTransfer'--调拨申请单
                                INNER JOIN T_STK_STKTRANSFERAPPENTRY E ON D.FID=E.FID AND C.FSID = E.FENTRYID  --调拨申请单明细
                                INNER JOIN T_BD_MATERIALBASE F ON E.FMATERIALID = F.FMATERIALID AND F.FERPCLSID='1'--物料基本表");
                    DBUtils.Execute(this.Context, sql2);

                    //更新分布式调入单据的含税金额，含税单价
                    string sql3 = string.Format(@"/*dialect*/UPDATE T_STK_STKTRANSFERINENTRY SET F_QLTK_HSPRICE = b.F_QLTK_HSPRICE,F_QLTK_HSAmount=b.F_QLTK_HSAmount
                                    FROM T_STK_STKTRANSFERIN RIN --分布式调入单据
                                    INNER JOIN T_STK_STKTRANSFERINENTRY RINE ON RIN.FID = RINE.FID --分布式调入单据明细
                                    INNER JOIN T_STK_STKTRANSFERINENTRY_LK RINEL ON RINE.FENTRYID = RINEL.FENTRYID AND RINEL.FRULEID='STK_TRANSFEROUT-STK_TRANSFERIN' --分布式调入单明细关联表
                                    INNER JOIN T_STK_STKTRANSFEROUT  A ON RINEL.FSBILLID = A.FID--分布式调出单
                                    INNER JOIN T_STK_STKTRANSFEROUTENTRY B ON A.FID = B.FID AND RINEL.FSID = B.FENTRYID--分布式调出单明细
                                    INNER JOIN T_STK_STKTRANSFEROUTENTRY_LK C ON B.FENTRYID=C.FENTRYID AND C.FRULEID='STK_TRANSFERAPPLY-STK_TRANSFEROUT'--分布式调出单明细关联表
                                    INNER JOIN T_STK_STKTRANSFERAPP D ON C.FSBILLID = D.FID AND D.F_QLTK_Combo_tzk='2' AND D.FTRANSTYPE='OverOrgTransfer'--调拨申请单
                                    INNER JOIN T_STK_STKTRANSFERAPPENTRY E ON D.FID=E.FID AND C.FSID = E.FENTRYID  --调拨申请单明细
                                    INNER JOIN T_BD_MATERIALBASE F ON E.FMATERIALID = F.FMATERIALID AND F.FERPCLSID='1'--物料基本表");
                    DBUtils.Execute(this.Context, sql3);
                }
            }
        }
    }
}
